<?php
//default sorting for categories list
if(!$_SESSION['categories_sort']){
	$e=explode("|", $conf['categories_sort']);
	$_SESSION['categories_sortby']=string_template($e[0], array('lang'=>$_SESSION['lang_short']));
	$_SESSION['categories_sort']=$e[1];
}
//

function list_categories($n, $depth, $param, $x, &$cont, $catid) {
	if($n != $catid || !$catid) {
		global $conf;

		SQLvalidate($n);

		$db = new dbquery;
		$db->cache('subcats_all_'.$n.'_'.$_SESSION['categories_sortby'].'_'.$_SESSION['categories_sort'], 'categories');
		$db->query("SELECT * FROM $conf[prefix]categories WHERE parent = $n ORDER by $_SESSION[categories_sortby] $_SESSION[categories_sort]") or $db->err(__FILE__, __LINE__);

		while($cat = $db->fetch_object()) {
			$t = "desc_$_SESSION[lang_short]";
			$tl = "name_$_SESSION[lang_short]";

			if($cat->$t == '' or $cat->$tl == '') {
				$tl = "name_".$conf['language'];
				$t = "desc_".$conf['language'];
			}

			$name = read_text_rest($cat->$tl);
			$desc = read_text_rest($cat->$t);

			if($param == 'admin') {
				$news='news_'.$_SESSION['lang_short'];
				$article='article_'.$_SESSION['lang_short'];
				$pages='pages_'.$_SESSION['lang_short'];
				$cont .= string_template(read_file('admin/themes/categories_item.php'), array('theme_path'=>$GLOBALS['theme_path'], 'id' => $cat->id, 'x' => $x, 'depth' => $depth, 'name' => $name, 'desc' => add_dots($desc, 20), 'news' => $cat->$news, 'article' => $cat->$article, 'pages' => $cat->$pages));
			}
			elseif($param=='select') {
				$cont .= '<option value="'.$cat->id.'"';
				if($cat->id == $catid)
				$cont .= ' selected';
				$cont .= '>';
				for($i = 0; $i < $depth; $i++) $cont .= '&nbsp;&nbsp;&nbsp;';
				$cont .= $name."</option>\n";
			}
			elseif($cat->id != $catid || !$catid) {
				$cont .= '<option value="'.$cat->id.'"';
				if($cat->id == $param)
				$cont .= ' selected';
				$cont .= '>';
				for($i = 0; $i < $depth; $i++) $cont .= '&nbsp;&nbsp;&nbsp;';
				$cont .= $name."</option>\n";
			}
			$x=($x%2)+1; 
			list_categories($cat->id, ($depth+1), $param, $x, $cont, $catid);
		}
	}
}

//cat link - generates link menu (Main > Cat1 > Subcat1)
function cat_get_link($cat, $what, $template, $spacer=' &gt; ', $limit=15, $addMainCat=true){
	global $conf;
	lang_include('comments');

	$db=new dbquery;

	SQLvalidate($cat);

	$i = $cat;
	$link=array();
	$x=1;
	while($i != 0) {
		$db->cache('cat_get_link_'.$i, 'categories');
		$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $i") or $db->err(__FILE__, __LINE__);

		$d = $db->fetch_object(true);

		$t = "desc_$_SESSION[lang_short]";
		$tl = "name_$_SESSION[lang_short]";

		if($d->$t == '' or $d->$tl == '') {
			$t = "desc_$conf[language]";
			$tl = "name_$conf[language]";
		}

		$name_ = $d->$tl;

		$link[] .= string_template($template, array('url'=>'index.php?module='.$what.'&amp;cat='.$i, 'id'=>$i, 'name'=>$name_, 'spacer'=>''));
		$i = $d->parent;
		$x++;
		if($x>$limit) break;
	}
	
	$link=array_reverse($link);
	$link=implode($spacer, $link);
	
	if($addMainCat)
		$link = string_template($template, array('url'=>'index.php?module='.$what, 'name'=>$lang['cat_main'], 'spacer'=>'')).$spacer.$link;
	
	return $link;
}
//

//cat link - with modrewrie on
function cat_get_link_mr($cat, $what, $template, $spacer=' &gt; ', $limit=15, $addMainCat=true){
	global $conf, $lang;

	$db=new dbquery;

	SQLvalidate($cat);

	$i = $cat;
	$link=array();
	$url='';
	$x=1;
	while($i != 0) {
		$db->cache('cat_get_link_'.$i, 'categories');
		$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $i") or $db->err(__FILE__, __LINE__);
		$d = $db->fetch_object(true);

		$t = "desc_$_SESSION[lang_short]";
		$tl = "name_$_SESSION[lang_short]";

		if($d->$t == '' or $d->$tl == '') {
			$t = "desc_$conf[language]";
			$tl = "name_$conf[language]";
		}

		$name_ = $d->$tl;
		$url=$what.'-'.read_text_mr($name_).'_'.$i.'.html';
		$link[] = string_template($template, array('url'=>$url, 'id'=>$i, 'name'=>$name_, 'spacer'=>''));
		$i = $d->parent;
		$x++;
		if($x>$limit) break;
	}
	
	$link=array_reverse($link);
	$link=implode($spacer, $link);
	
	if($addMainCat)
		$link = string_template($template, array('url'=>$what.'.html', 'name'=>$lang['cat_main'], 'spacer'=>'')).$spacer.$link;
	
	return $link;
}
//

//funkcja zwraca do mod_rewrite'a $what-nazwakat_$id_
function cat_get_mr($id_, $what) {
	global $conf;
	$db=new dbquery;
  
	if(!$conf['mod_rewrite'])
		return 'index.php?module='.$what.'&amp;cat='.$id_;
  
	SQLvalidate($id_);

	$db->cache('cat_get_link_'.$id_, 'categories');
	$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $id_") or $db->err(__FILE__, __LINE__);
	$d = $db->fetch_object(true);

	$tl = "name_$_SESSION[lang_short]";

	if($d->$tl == '') {
		$tl = "name_$conf[language]";
	}

	return $what.'-'.read_text_mr($d->$tl).'_'.$d->id.'.html';
}

//returns category info
function get_cat_by_id($id_) {
	global $conf;

	SQLvalidate($id_);

	$db = new dbquery;
	$db->cache('cat_get_link_'.$id_, 'categories');
	$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $id_") or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object(true);

	return $d;
}
//

function get_cat_name_by_id($id_){
	global $conf;

	SQLvalidate($id_);

	$db = new dbquery;
	$db->cache('cat_get_link_'.$id_, 'categories');
	$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $id_") or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object(true);

	$tl = "name_$_SESSION[lang_short]";
	if($d->$tl == '') {
		$tl = "name_".$conf['language'];
	}

	return read_text_rest($d->$tl);
	//
}

//updates values in all parent categories
//$i = subcategory, $increase (1 for +, 0 for -), if $module is set, the function will [in|de]crease value by 1 for that module
function cat_update_values($i, $increase, $module = NULL, $q=1) {
	global $conf;

	if(!perms_check('categories', 'edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit();
	}	
	
	SQLvalidate($i);

	if($increase == 1)
	$what = '+';
	else
	$what = '-';

	$db = new dbquery;

	if(!$module) {
		$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $i") or $db->err(__FILE__, __LINE__);
		$d = $db->fetch_object();

		$sql=array();
		$x=2;
		while($f = @$db->fetch_field($x)){
			$name=$f->name;
			
			if(strpos($name, 'name_')!==false or strpos($name, 'desc_')!==false) { $x++; continue; }

			$sql[]=$name.'='.$name.$what.$d->$name;
			$x++;
		}
		$sql=implode(', ', $sql);

		$i = $d->parent;
	}
	
	$tpl = new tpl;
	do {
		if(!$module)
		$db->query("UPDATE $conf[prefix]categories SET $sql WHERE id = $i") or $db->err(__FILE__, __LINE__);
		else
		$db->query("UPDATE $conf[prefix]categories SET $module=$module $what $q WHERE id = $i") or $db->err(__FILE__, __LINE__);
		
		$db->clear_cache('subcats_'.$i.'*', 'categories');
		$db->clear_cache('subcats_all_'.$i.'*', 'categories');
		$db->clear_cache('cat_menu_list_*_'.$i.'_*.cache', 'categories');
		$db->clear_cache('cat_menu_list_num_rows_*_'.$i.'_*.cache', 'categories');
		$db->clear_cache('cat_get_link_'.$i.'.cache', 'categories');
		$db->clear_cache('cat_'.$i.'.cache', 'categories');
		$tpl->clear_cache('*_cat-'.$i.'*');
		
		if($i==0) $i=-1;
		else {
			$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $i") or $db->err(__FILE__, __LINE__);
			$d = $db->fetch_object();
			$i = $d->parent;
		}
	} while($i != -1);
}

//zwraca tablice id subkategorii danej kategorii ($id_)
function get_subcategories($id_) {
	global $conf;

	SQLvalidate($id_);

	$array=array($id_);

	$db = new dbquery;
	$db2 = new dbquery;
	$db->cache('subcats_all_'.$id_.'_'.$_SESSION['categories_sortby'].'_'.$_SESSION['categories_sort'], 'categories');
	$db->query("SELECT * FROM $conf[prefix]categories WHERE parent = $id_ ORDER by $_SESSION[categories_sortby] $_SESSION[categories_sort]") or $db->err(__FILE__, __LINE__);

	while($d=$db->fetch_object()) {
		$array[]=$d->id;
		$db2->cache('subcats_all_'.$d->id.'_'.$_SESSION['categories_sortby'].'_'.$_SESSION['categories_sort'], 'categories');
		$db2->query("SELECT * FROM $conf[prefix]categories WHERE parent = $d->id ORDER by $_SESSION[categories_sortby] $_SESSION[categories_sort]") or $db2->err(__FILE__, __LINE__);

		if($db2->num_rows(false, true))
		$array=array_merge($array, get_subcategories($d->id));
	}

	return $array;
}

//fukncja rekurencyjna do wyswietlanaia menu
function get_cat_menu_list($id_, $depth, $template, $spacer, $limit=NULL, $what, $sortby=NULL, $sort='ASC') {
	global $conf, $lang;

	SQLvalidate($id_);
	SQLvalidate($limit);

	if(!$sortby)
	$sortby='name_'.$_SESSION['lang_short'];

	$db = new dbquery;
	$db->cache('cat_menu_list_'.$what.'_'.$id_.'_'.$sortby.'_'.$sort, 'categories');
	$db->query("SELECT * FROM $conf[prefix]categories WHERE parent = $id_ && ".$what."_".$_SESSION['lang_short']." > 0 ORDER by $sortby $sort") or $db->err(__FILE__, __LINE__);
	$content='';

	$j = 0;
	while($cat_ = $db->fetch_object()) {
		$spacer_='';
		for($i=0;$i<$depth;$i++)
		$spacer_.=$spacer;

		$t = 'desc_'.$_SESSION['lang_short'];
		$tl = 'name_'.$_SESSION['lang_short'];
		$count = $what.'_'.$_SESSION['lang_short'];

		if($cat_->$tl == '') {
			$tl = 'name_'.$conf['language'];
			$t = 'desc_'.$conf['language'];
			$count = $what.'_'.$conf['language'];
		}

		$name = read_text_rest($cat_->$tl);
		$desc = read_text_rest($cat_->$t);

		$GLOBALS['what']=$what;
		$GLOBALS['cat_']=$cat_;
		$GLOBALS['conf']=$conf;
		$GLOBALS['name']=$name;
		plugins('std/mod_rewrite/1/');

		if($limit)
		$name=add_dots_($name, $limit);

		if(is_array($template)) {
			if(!$template[$depth-1])
			$tmp=end($template);
			else
			$tmp=$template[$depth-1];
		}
		else
		$tmp=$template;

		$content.=string_template($tmp, array('name' => $name, 'id' => $cat_->id, 'desc' => $desc, 'spacer' => $spacer_, 'count'=>$cat->$count, 'url' => $GLOBALS['url']));

		$db2 = new dbquery;
		$db2->cache('cat_menu_list_'.$what.'_'.$cat_->id_.'_'.$sortby.'_'.$sort, 'categories');
		$db2->query("SELECT * FROM $conf[prefix]categories WHERE parent = $cat_->id && ".$what."_".$_SESSION['lang_short']." > 0 ORDER by $sortby $sort") or $db2->err(__FILE__, __LINE__);

		if($db2->num_rows(true)!=0)
		$content.=get_cat_menu_list($cat_->id, ($depth+1), string_template($template, array('j' => $j++)), $spacer, $limit, $what, $sortby, $sort);
	}
	return $content;
}

//wyswietla <OPTIONS> kategorii do wyszukiwarki
function print_search_cat($cat=0, $prefix='', $depth=0, $limit='download'){
	global $conf;

	SQLvalidate($cat);

	$opts='';

	$db = new dbquery;
	$db2 = new dbquery;

	$tl = "name_$_SESSION[lang_short]";

	//opts
	$db->query("SELECT * FROM $conf[prefix]categories WHERE parent=$cat && $limit > 0") or $db->err(__FILE__, __LINE__);
	while($d=$db->fetch_object()) {

		$opts.=("<option value='".$d->id."'>".$prefix.read_text_rest($d->$tl)."</option>");

		$db2->query("SELECT * FROM $conf[prefix]categories WHERE parent = $d->id && $limit > 0") or $db2->err(__FILE__, __LINE__);
		if($db2->num_rows())
		$opts.=print_search_cat($d->id, $prefix.'&nbsp;&nbsp;', $depth+1, $limit);
	}

	if($cat==0)
	echo $opts;
	else
	return $opts;
}

function categories_field_add($module='') {
	if(!$module) exit;
	
	global $conf;
	
	if(perms_check('admin', 'modules')) {
		$db = new dbquery;
		$db->query("SELECT * FROM $conf[prefix]lang_fields WHERE `table`='categories'") or $db->err(__FILE__, __LINE__);
		
		$d=$db->fetch_object();
		$fields=explode(";", $d->fields);
		$f=$fields[0];
		unset($fields[0]);
		
		$types=explode(";", $d->types);
		$defaults=explode(";", $d->defaults);
		//$fields=array_slice($fields, 0, count($fields)-2);
		if(!in_array($module.'_{lang}', $fields)) {
			$fields=$f.';'.$module.'_{lang};'.implode(";", $fields);
			$types='INT(8);'.implode(";", $types);
			$defaults='0;'.implode(";", $defaults);
			
			$db->query("UPDATE $conf[prefix]lang_fields SET `fields`='".$fields."', `types`='".$types."', `defaults`='".$defaults."' WHERE `table`='categories'");
			
			$langs=get_lang_list();
			foreach($langs as $l) {
				$db->query("ALTER TABLE $conf[prefix]categories ADD `".$module."_".$l."` INT(8) NOT NULL DEFAULT '0' AFTER `news_".$l."`") or $db->err(__FILE__, __LINE__);
			}
		}
		
		$db->clear_cache('*', 'categories/');
	}
}

function categories_field_delete($module='') {
	if(!$module) exit;
	
	global $conf;
	
	if(perms_check('admin', 'modules')) {
		$db = new dbquery;
		$db->query("SELECT * FROM $conf[prefix]lang_fields WHERE `table`='categories'") or $db->err(__FILE__, __LINE__);
		$d=$db->fetch_object();
		
		$fields=explode(";", $d->fields);
		$f=array_search($module.'_{lang}', $fields);
		unset($fields[$f]);
		
		$types=explode(";", $d->types);
		unset($types[$f]);
		
		$defaults=explode(";", $d->defaults);
		unset($defaults[$f]);
		
		$fields=implode(";", $fields);
		$types=implode(";", $types);
		$defaults=implode(";", $defaults);
			
		$db->query("UPDATE $conf[prefix]lang_fields SET `fields`='".$fields."', `types`='".$types."', `defaults`='".$defaults."' WHERE `table`='categories'") or $db->err(__FILE__, __LINE__);
			
		$langs=get_lang_list();
		foreach($langs as $l) {
			$db->query("ALTER TABLE $conf[prefix]categories DROP COLUMN ".$module."_".$l);
		}
		
		$db->clear_cache('*', 'categories/');
	}
}
?>
